<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
  <meta name="generator" content=
  "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">

  <title>strcmp</title>
  <link href="../cppreference.css" rel="stylesheet" type="text/css">
</head>

<body>
<table>
  <tr>
  <td>
  <div class="body-content">

  <div class="header-box">
    <a href="../index.html">cppreference.com</a> &gt; <a href=
    "index.html">Standard C String and Character</a> &gt; <a href=
    "strcmp.html">strcmp</a>
  </div>

  <div class="name-format">
    strcmp
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;string.h&gt;
  int strcmp( const char *str1, const char *str2 );
</pre>

  <p>The function strcmp() compares <em>str1</em> and <em>str2</em>,
  then returns:</p>

  <table class="code-table">
    <tr>
      <th class="code-table-th">Return value</th>

      <th class="code-table-th">Explanation</th>
    </tr>

    <tr>
      <td class="code-table-td">less than 0</td>

      <td class="code-table-td">&#39;&#39;str1&#39;&#39; is less than
      &#39;&#39;str2&#39;&#39;</td>
    </tr>

    <tr>
      <td class="code-table-td">equal to 0</td>

      <td class="code-table-td">&#39;&#39;str1&#39;&#39; is equal to
      &#39;&#39;str2&#39;&#39;</td>
    </tr>

    <tr>
      <td class="code-table-td">greater than 0</td>

      <td class="code-table-td">&#39;&#39;str1&#39;&#39; is greater
      than &#39;&#39;str2&#39;&#39;</td>
    </tr>
  </table>

  <p>For example:</p>
  <pre class="example-code">
   printf( &quot;Enter your name: &quot; );
   scanf( &quot;%s&quot;, name );
   if( strcmp( name, &quot;Mary&quot; ) == 0 ) {
     printf( &quot;Hello, Dr. Mary!\n&quot; );
   }        
</pre>

  <p>Note that if <em>str1</em> or <em>str2</em> are missing a
  null-termination character, then strcmp() may not produce valid
  results.  For a similar (and safer) function that includes explicit
  bounds checking, see <a href="strncmp.html">strncmp()</a>.</p>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    <a href="memcmp.html">memcmp</a><br>
    <a href="strcat.html">strcat</a><br>
    <a href="strchr.html">strchr</a><br>
    <a href="strcoll.html">strcoll</a><br>
    <a href="strcpy.html">strcpy</a><br>
    <a href="strlen.html">strlen</a><br>
    <a href="strncmp.html">strncmp</a><br>
    <a href="strxfrm.html">strxfrm</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
